package fudan.se.project.testutil;

import fudan.se.project.domain.User;
import fudan.se.project.domain.conference.Conference;
import fudan.se.project.domain.conference.role.Author;
import fudan.se.project.domain.conference.role.Chair;
import fudan.se.project.domain.conference.role.PcMember;
import fudan.se.project.repository.entity.UserRepository;
import fudan.se.project.repository.entity.conference.ConferenceRepository;
import fudan.se.project.repository.entity.conference.role.AuthorRepository;
import fudan.se.project.repository.entity.conference.role.ChairRepository;
import fudan.se.project.repository.entity.conference.role.PcMemberRepository;

import java.util.ArrayList;

public class UserAndConferenceTestUtil {


    private ConferenceRepository conferenceRepository;

    private UserRepository userRepository;


    private ChairRepository chairRepository;

    private PcMemberRepository pcMemberRepository;

    private AuthorRepository authorRepository;

    private ArrayList<User> users = new ArrayList<>();
    private ArrayList<Conference> conferences = new ArrayList<>();

    public UserAndConferenceTestUtil(ConferenceRepository conferenceRepository,
                                     UserRepository userRepository,
                                     ChairRepository chairRepository,
                                     PcMemberRepository pcMemberRepository,
                                     AuthorRepository authorRepository) {
        this.conferenceRepository = conferenceRepository;
        this.userRepository = userRepository;
        this.chairRepository = chairRepository;
        this.pcMemberRepository = pcMemberRepository;
        this.authorRepository = authorRepository;
    }

    public User getUserByIndex(int index) {
        return userRepository.findById(users.get(index).getId()).get();
    }


    public Conference getConferenceByIndex(int index) {
        return conferenceRepository.findById(conferences.get(index).getId()).get();
    }

    public void addChair(int conferenceIndex, int userIndex) {
        this.chairRepository.save(
                new Chair(getUserByIndex(userIndex), getConferenceByIndex(conferenceIndex)));
    }

    public void addPcMember(int conferenceIndex, int userIndex) {
        this.pcMemberRepository.save(
                new PcMember(getUserByIndex(userIndex), getConferenceByIndex(conferenceIndex)));
    }

    public void addPcMember(int conferenceIndex, int... userIndices) {
        for (int i = 0; i < userIndices.length; i++) {
            addPcMember(conferenceIndex, userIndices[i]);
        }
    }

    public void addAuthor(int conferenceIndex, int userIndex) {
        this.authorRepository.save(
                new Author(getUserByIndex(userIndex), getConferenceByIndex(conferenceIndex)));
    }

    public void addAuthor(int conferenceIndex, int... userIndices) {
        for (int i = 0; i < userIndices.length; i++) {
            addAuthor(conferenceIndex, userIndices[i]);
        }
    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public ArrayList<Conference> getConferences() {
        return conferences;
    }
}
